package com.songyang.serialize;

import com.songyang.common.ErrorMsg;
import com.songyang.common.ExceptionHandler;
import com.songyang.common.YoungSQLException;
import com.songyang.serialize.inter.YoungSqlSerialize;

import java.util.HashMap;

/**
 * @ClassName SerializeFactory
 * @Description 序列化工厂
 * @date 2022/12/30 11:46
 * @Author yanceysong
 * @Version 1.0
 */
public class SerializeFactory {
    private static final HashMap<Integer, YoungSqlSerialize> map = new HashMap<>();

    static {
        map.put(YoungSqlSerialize.YOUNG_SERIALIZE, new YoungSqlEncoder());
        map.put(YoungSqlSerialize.KRYO_SERIALIZE, new KryoEncoder());
    }

    /**
     * 根据配置文件获取一个解码器
     *
     * @param decoderType 解码器的类型
     * @return 解码器
     */
    public static YoungSqlSerialize getDecoder(int decoderType) {
        if (!map.containsKey(decoderType)) {
            try {
                throw new YoungSQLException(ErrorMsg.ILLEGAL_ENCODER_TYPE);
            } catch (YoungSQLException e) {
                ExceptionHandler.handler(e);
            }
        }
        return map.get(decoderType);
    }

    /**
     * 根据配置文件获取一个解码器
     *
     * @return 解码器
     */
    public static YoungSqlSerialize getDecoder() {
        return getDecoder(YoungSqlSerialize.YOUNG_SERIALIZE);
    }
}
